package cn.sjlink.appraise.controller;

import cn.sjlink.appraise.constants.AppraiseItemStatus;
import cn.sjlink.appraise.form.*;
import cn.sjlink.appraise.service.AppraiseGoodsService;
import cn.sjlink.config.BusinessException;
import cn.sjlink.config.LoginException;
import cn.sjlink.config.ResultHandler;
import cn.sjlink.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;

import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

@CrossOrigin("*")
@RestController
@RequestMapping(value = "appraise")
public class AppraiseController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AppraiseGoodsService appraiseGoodsService;

    //userId  1186920210107172915  token 8016cf51ffc5bf7421554cd8439feba3

    @RequestMapping(value = "getChoice",method = RequestMethod.GET)
    public Result getChoice(){
        return new Result(AppraiseItemStatus.productItems);
    }

    @RequestMapping(value = "submit",method = RequestMethod.POST)
    public Result submit(@RequestBody AppraiseForm appraiseForm,@RequestHeader HttpHeaders headers){
        try {
            String redisToken = redisUtil.get(String.valueOf(appraiseForm.getUserId())) ==null?"":redisUtil.get(String.valueOf(appraiseForm.getUserId())).toString();
            TokenUtil.checkToken(headers.getFirst("token"),redisToken);
            return appraiseGoodsService.submitAppraise(appraiseForm);
        } catch (LoginException e) {
            return new Result(ResultCode.LOGIN,e.getMessage());
        }
    }

    @RequestMapping(value = "ensureResult",method = RequestMethod.POST)
    @ResultHandler
    public Result ensureResult(@RequestBody AppraiseEnsureForm appraiseEnsureForm,@RequestHeader HttpHeaders headers)throws Exception{
        String redisToken = redisUtil.get(String.valueOf(appraiseEnsureForm.getUserId())) ==null?"":redisUtil.get(String.valueOf(appraiseEnsureForm.getUserId())).toString();
        TokenUtil.checkToken(headers.getFirst("token"),redisToken);
        return appraiseGoodsService.ensureAppraise(appraiseEnsureForm);
    }

    @RequestMapping(value = "waitSendList",method = RequestMethod.GET)
    public Result waitSend(Long userId,Integer page,Integer pageSize,@RequestHeader HttpHeaders headers){
        try {
            String redisToken = redisUtil.get(String.valueOf(userId)) ==null?"":redisUtil.get(String.valueOf(userId)).toString();
            TokenUtil.checkToken(headers.getFirst("token"),redisToken);
            return appraiseGoodsService.waitSendList(userId, page, pageSize);
        } catch (LoginException e) {
            return new Result(ResultCode.LOGIN,e.getMessage());
        }
    }
    @RequestMapping(value = "putFinish",method = RequestMethod.POST)
    public Result putFinish(@RequestBody PutFinishForm putFinishForm,@RequestHeader HttpHeaders headers){
        try {
            String redisToken = redisUtil.get(String.valueOf(putFinishForm.getUserId())) ==null?"":redisUtil.get(String.valueOf(putFinishForm.getUserId())).toString();
            TokenUtil.checkToken(headers.getFirst("token"),redisToken);
            return appraiseGoodsService.putFinish(putFinishForm);
        } catch (LoginException e) {
            return new Result(ResultCode.LOGIN,e.getMessage());
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "checkCode",method = RequestMethod.POST)
    public Result checkCode(@RequestBody PickCodeForm pickCode){
        try {
            return appraiseGoodsService.checkCode(pickCode.getPickCode(), pickCode.getStackNo(), pickCode.getSign());
        } catch (BusinessException e) {
            return new Result(e.getMsg());
        }
    }

    @RequestMapping(value = "pickNotice",method = RequestMethod.POST)
    public Result pickNotice(@RequestBody PickCodeForm pickCode){
        try {
            return appraiseGoodsService.pickNotice(pickCode.getPickCode(), pickCode.getStackNo(), pickCode.getSign());
        } catch (Exception e) {
            return new Result(false,"开锁异常，请重试");
        }
    }


    @RequestMapping(value = "uploadImage",method = RequestMethod.POST)
    public Result uploadImage(@RequestBody UploadForm uploadForm,@RequestHeader HttpHeaders headers){
        try {
            String redisToken = redisUtil.get(String.valueOf(uploadForm.getUserId())) ==null?"":redisUtil.get(String.valueOf(uploadForm.getUserId())).toString();
            TokenUtil.checkToken(headers.getFirst("token"),redisToken);
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] imageByte =base64Decoder.decodeBuffer(uploadForm.getImageStr());
            SerialBlob serialBlob = new SerialBlob(imageByte);
            InputStream binaryStream = serialBlob.getBinaryStream();
            UploadClietUtil uploadClietUtil = new UploadClietUtil();
            uploadClietUtil.setFiledir(DateUtil.getSNowDate()+"/");
            String uploadImg = uploadClietUtil.uploadFile2OSS(binaryStream, DateUtil.getDefaultDate() + ".jpeg");
            String[] split = uploadImg.split("\\?");
            String imgUrl = split[0];
            return new Result(true, "上传成功", imgUrl);
        } catch (LoginException e) {
            return new Result(ResultCode.LOGIN,e.getMessage());
        }catch (SQLException e) {
            return new Result(false,"上传失败，请重试");
        }catch (IOException e) {
            return new Result(false,"上传失败，请重试");
        }
    }


}
